/*
 * Copyright (c) 2020-2099 the original author or authors. All rights reserve.
 */
package org.tianyun.cloud.amqp.rabbitmq.autoconfigure;

import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.tianyun.cloud.amqp.rabbitmq.RabbitContainerRegistrar;
import org.tianyun.cloud.amqp.rabbitmq.RabbitMqConnectionFactoryConfigurer;
import org.tianyun.cloud.amqp.rabbitmq.RabbitMqContainerPostProcessor;
import org.tianyun.cloud.amqp.rabbitmq.advice.ContextAdvice;
import org.tianyun.cloud.amqp.rabbitmq.annotation.RabbitContainer;
import org.tianyun.cloud.amqp.rabbitmq.configuration.RabbitMqProperties;

import java.util.List;

/**
 * RabbitMqAutoConfiguration
 *
 * @auther ebert_chan
 */
@Configuration(proxyBeanMethods = false)
@Import(RabbitContainerRegistrar.class)
public class RabbitMqAutoConfiguration {

    public static final String RABBIT_CONNECTION_FACTORY_BEAN = "rabbitConnectionFactory";

    public static final String RABBIT_LISTENER_CONTAINER_FACTORY_BEAN = "rabbitListenerContainerFactory";

    public static final String RABBIT_TEMPLATE_BEAN = "rabbitTemplate";

    public static final String MESSAGE_CONVERTER_BEAN = "messageConverter";

    @Bean
    public static RabbitMqContainerPostProcessor rabbitMqContainerPostProcessor(
            ObjectProvider<List<RabbitMqConnectionFactoryConfigurer>> rabbitMqConnectionFactoryConfigurerProvider) {
        return new RabbitMqContainerPostProcessor(rabbitMqConnectionFactoryConfigurerProvider);
    }

    @Bean(name = MESSAGE_CONVERTER_BEAN)
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    @RabbitContainer(prefix = "rabbitmq.common", primary = true, connectionFactory = RABBIT_CONNECTION_FACTORY_BEAN, containerFactory = RABBIT_LISTENER_CONTAINER_FACTORY_BEAN, rabbitTemplate = RABBIT_TEMPLATE_BEAN)
    public RabbitMqProperties commonRabbitMqProperties() {
        return new RabbitMqProperties();
    }

    @Bean
    public ContextAdvice contextAdvice() {
        return new ContextAdvice();
    }

}
